package adri.exam.struts;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class ProveedorDAO {

	@SessionTarget
	Session session;
	
	@TransactionTarget
	Transaction transaction;
	

	/**
	 * Used to save or update a user.
	 */
	public void GuardarOActualizarProveedor(Proveedor proveedor) {
		try {
			session.saveOrUpdate(proveedor);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	/**
	 * Used to delete a user.
	 */
	public void eliminarProveedor(Long id) {
		try {
			Proveedor proveedor = (Proveedor) session.get(Proveedor.class, id);
			session.delete(proveedor);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		} 
	}
	
	/**
	 * Used to list all the users.
	 */
	@SuppressWarnings("unchecked")
	public List<Proveedor> listarProveedores(){
		List<Proveedor> proveedores = null;
		try {
			proveedores = session.createQuery("from proveedor").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return proveedores;
	}

	/**
	 * Used to list a single user by Id.
	 */
	public Proveedor listarProveedorPorId(Long id) {
		Proveedor proveedor = null;
		try {
			proveedor = (Proveedor) session.get(Proveedor.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return proveedor;
	}

	
}
